var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList: '',
    carTotal: 0,
    allCheckStatus: 0,
    active2: '-active',
    isLoad: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    
  },
  onShow: function () {
    this.getCartList(1)
  },
  getCartList: function(init) {
    var _this = this
    util.djax({
      url: '/wxapp/Cart/Index',
    }).then(function(res){
      if (res.code == 1001) {
        if (res.data.length==0) {
          _this.setData({
            cartList: '',
            isLoad: 1
          })
          return false;
        }
        var cartList = _this.data.cartList
        var checkList = {}
        for (var i=0;i<cartList.length;i++) {
          checkList[cartList[i].id] = cartList[i].check
        }
        var cartList = res.data
        for (var i=0; i<cartList.length; i++) {
          // cartList[i].check = checkList[cartList[i].id]
          cartList[i].check = 1
        }
        _this.setData({
          cartList: cartList
        })
       
        if (init != undefined) {
          var cartStatus = wx.getStorageSync('cartIds')
          
          if (cartStatus && JSON.stringify(cartStatus) != '{}') {
            var totalMoney = 0;
            for (var i = 0; i < cartList.length; i++) {
              // if (cartStatus[cartList[i].id]) {
                cartList[i].check = 1
                totalMoney += cartList[i].price * cartList[i].number
              // }
            }
            _this.setData({
              cartList: cartList,
              carTotal: totalMoney
            })
          } else {
            _this.allChack()
          }  
          

        } else {
          var totalMoney = 0;
          for (var i = 0; i < cartList.length; i++) {
            if (cartList[i].check == 1) {
              totalMoney += cartList[i].price * cartList[i].number
            }
          }
          _this.setData({
            cartList: cartList,
            carTotal: totalMoney
          })
        }
      }
      _this.setData({
        isLoad: 1
      })
    })
  },
  initCheck: function (id,doWhat) {
    var cartStatus = wx.getStorageSync('cartIds')
    cartStatus = cartStatus ? cartStatus:{}
    if (doWhat) {
      cartStatus[id] = 1
    } else {
      delete cartStatus[id]
    }
    wx.setStorageSync('cartIds', cartStatus)
  },
  delCarList: function (e) {
    var id = e.currentTarget.dataset.id
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用删除方法了
          util.djax({
            url: '/wxapp/Cart/del',
            data: {
              id: id
            }
          }).then(function (res) {
            _this.getCartList()
          })
        } 
      }
    }) 
  },
  bindKeyInput: function (e) {
    var id = e.currentTarget.dataset.id
    var counts = e.currentTarget.dataset.counts
    var count = e.detail.value
    var lower = e.currentTarget.dataset.lower
    var _this = this
    if (count < lower) {
      wx.showToast({
        title: '不能小于最低起售',
        icon: 'none',
        duration: 2000,
      })
      setTimeout(function () {
        _this.getCartList(1)
      }, 2000)
    }else{
      this.editCart(id, count, 1)
    }
  },
  addCart: function (e) {
    var id = e.currentTarget.dataset.id
    var count = e.currentTarget.dataset.count
    this.editCart(id,1,0)
  },
  reduceCart: function (e) {
    var id = e.currentTarget.dataset.id
    var count = e.currentTarget.dataset.count
    var lower = e.currentTarget.dataset.lower+1
    console.log(lower)
    console.log(count)
    if (count < lower) {
      wx.showToast({
        title: '不能小于最低起售',
        icon: 'none',
        duration: 2000,
      })
    }else{
      this.editCart(id, -1, 0)
    }
    
  },
  editCart: function (id,number,c) {
    var _this = this
    util.djax({
      url: '/wxapp/Cart/addEdit',
      data: {
        a: id,
        b: number,
        c:c
      }
    }).then(function (res) {
      if (res.code != 1001) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        return
      } else {
        _this.getCartList()
      }
    })    
  },
  checkAction: function (e) {
      var id = e.currentTarget.dataset.id
      var check = e.currentTarget.dataset.check
      check = check?0:1 
    this.initCheck(id,'del')
      var cartList = this.data.cartList
      var totalMoney = 0;
      for (var i=0;i<cartList.length;i++) {
        if (cartList[i].id==id) {
          cartList[i].check = check
        }
        if (cartList[i].check==1) {
          totalMoney += cartList[i].price * cartList[i].number
        }
      }
      this.setData({
        cartList: cartList,
        carTotal: totalMoney
      })
      
  },
  allChack: function(e) {
    var totalMoney = 0;
    var cartList = this.data.cartList
    var check = this.data.allCheckStatus?0:1
    for (var i = 0; i < cartList.length; i++) {
      cartList[i].check = check
      if (cartList[i].check == 1) {
        totalMoney += cartList[i].price * cartList[i].number
      }
      this.initCheck(cartList[i].id, check?'':'del')
    }
    this.setData({
      cartList: cartList,
      carTotal: totalMoney,
      allCheckStatus: check
    })
  },
  nextStep: function() {
      var wantBuy = []
      var cartList = this.data.cartList
      for (var i = 0; i < cartList.length; i++) {
          if (cartList[i].check==1) {
            wantBuy.push(cartList[i])
          }
      }
    if (wantBuy.length==0) {
      wx.showToast({
        title: '请最少选择一个商品!',
        icon: 'none'
      })
      return false
    }
      wx.setStorageSync('wantBuy', wantBuy)
      wx.navigateTo({
        url: "../shopList/index"
      })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})